package com.longe.netty.transferFiles.util;

import com.longe.netty.transferFiles.domain.TransferConstants;
import com.longe.netty.transferFiles.domain.TransferData;
import com.longe.netty.transferFiles.domain.TransferInstruct;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class FileUtil {

    public static TransferData readFile(String fileUrl, Integer readPosition) throws IOException {
        File file = new File(fileUrl);
        //r: 只读模式 rw:读写模式
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        randomAccessFile.seek(readPosition);
        byte[] bytes = new byte[1024];
        int readSize = randomAccessFile.read(bytes);
        if (readSize <= 0) {
            randomAccessFile.close();
            //TransferConstants.FileStatus ｛0开始、1中间、2结尾、3完成｝
            return new TransferData(TransferConstants.FileStatus.COMPLETE);
        }
        TransferData transferData = new TransferData();
        transferData.setFileUrl(fileUrl);
        transferData.setFileName(file.getName());
        transferData.setBeginPosition(readPosition);
        transferData.setEndPosition(readPosition + readSize);
        //不足1024需要拷贝去掉空字节
        if (readSize < 1024) {
            byte[] copy = new byte[readSize];
            System.arraycopy(bytes, 0, copy, 0, readSize);
            transferData.setBytes(copy);
            transferData.setFileTransferStatus(TransferConstants.FileStatus.END);
        } else {
            transferData.setBytes(bytes);
            transferData.setFileTransferStatus(TransferConstants.FileStatus.CENTER);
        }
        randomAccessFile.close();
        return transferData;
    }

    public static TransferInstruct writeFile(String baseUrl, TransferData transferData) throws IOException {

        if (TransferConstants.FileStatus.COMPLETE == transferData.getFileTransferStatus()) {
            //TransferConstants.FileStatus ｛0开始、1中间、2结尾、3完成｝
            return new TransferInstruct(TransferConstants.FileStatus.COMPLETE);
        }

        File file = new File(baseUrl + "/" + transferData.getFileName());
        //r: 只读模式 rw:读写模式
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
        //移动文件记录指针的位置，
        // 调用了seek（start）方法，是指把文件的记录指针定位到start字节的位置。也就是说程序将从start字节开始写数据
        randomAccessFile.seek(transferData.getBeginPosition());
        randomAccessFile.write(transferData.getBytes());
        randomAccessFile.close();

        if (TransferConstants.FileStatus.END == transferData.getFileTransferStatus()) {
            //TransferConstants.FileStatus ｛0开始、1中间、2结尾、3完成｝
            return new TransferInstruct(TransferConstants.FileStatus.COMPLETE);
        }

        //文件分片传输指令
        TransferInstruct transferInstruct = new TransferInstruct();
        transferInstruct.setFileTransferStatus(TransferConstants.FileStatus.CENTER);
        transferInstruct.setFileUrl(transferData.getFileUrl());
        transferInstruct.setReadPosition(transferData.getEndPosition());//读取位置

        return transferInstruct;
    }
}